** Generating Tables and Plots of
** CCES Confidence Experiment regarding
** Iran attribution (Figure 2 in text)

*********************
* Stata preliminaries
*********************

clear all
set more off
version 14

global path "~/Dropbox/0001 Academic Projects/Ongoing/0148 The Cyber/Posted/FPA Version/Work"
cd "$path/Data/"
use "CCESUsable.dta"

set more off
set scheme plotplain

* Adding labels to prettify outputs
lab var cueCIA "CIA Treatment"
lab var cueAgree "Agreement Treatment"
lab var white "White"
lab var college "College"
lab var female "Female"
lab var threatiran "Perception of Iranian Threat"
lab var dem7 "Democrat"
lab var gop7 "Republican"
lab var ind7 "Independent"
lab var age "Age in Years"

lab def cueAgreelab2 0 "Divided Endorsement" 1 "Supportive Endorsement"
lab val cueAgree cueAgreelab2

lab def cueCIAlab 0 "Independent Experts" 1 "CIA Endorsement"
lab val cueCIA cueCIAlab

* generating the cueIndependent variable to create a more intuitive way of 
* interpreting the data

gen cueIndependent = .
replace cueIndependent =1 if cueCIA == 0
replace cueIndependent =0 if cueCIA == 1

lab def cueIndependentlab 0 "CIA Endorsement" 1 "Independent Endorsement"
lab val cueIndependent cueIndependentlab


************************
* Generating Figure 2
************************
cd "$path/Charts/"

* running and storing the OLS regressions 

reg confidenceiran i.cueAgree##i.cueIndependent 
eststo confidenceAllBase

reg confidenceiran i.cueAgree##i.cueIndependent college ///
	age white threatiran female i.dem7 i.gop7
eststo confidenceAllControls

reg confidenceiran i.cueAgree##i.cueIndependent college ///
	age white threatiran female if dem7==1
eststo confidenceDemControls

reg confidenceiran i.cueAgree##i.cueIndependent college ///
	age white threatiran female if gop7==1
eststo confidenceGOPControls

reg confidenceiran i.cueAgree##i.cueIndependent college ///
	age white threatiran female if ind7==1
eststo confidenceIndControls

* plotting the coefficients, all in one go

coefplot (confidenceAllControls, label(All) offset(.3)) ///
	(confidenceDemControls, label(Democrats) offset(.15)) ///
	(confidenceIndControls, label(Independents) ) ///
	(confidenceGOPControls, label(Republicans) offset(-.15)), ///
	title("Figure 2: Factors Influencing Confidence in Attribution of Cyberattack to Iran") ///
	subtitle("CCES 2018 Experiment; Selected OLS Coefficients Displayed", size(small)) ///
	drop(_cons female college age white threatiran *.dem7 *.gop7) coeflabels(,wrap(20)) ///
	note("Full specification includes demographic controls as well as controls for perception of Iranian threat.",size(vsmall)) ///
	xline(0)
	
graph export "Figure 2.pdf", replace

* running and storing alternate OLS regressions (note "vulnerablepower", a 
* measurement of whether R believes that power grids are vulnerable)

reg confidenceiran i.cueAgree##i.cueIndependent college ///
	age white threatiran female vulnerablepower ///
	i.dem7 i.gop7
eststo confidenceAllControls2

reg confidenceiran i.cueAgree##i.cueIndependent college ///
	age white threatiran female vulnerablepower ///
	if dem7==1
eststo confidenceDemControls2

reg confidenceiran i.cueAgree##i.cueIndependent college ///
	age white threatiran female vulnerablepower ///
	if gop7==1
eststo confidenceGOPControls2

reg confidenceiran i.cueAgree##i.cueIndependent college ///
	age white threatiran female vulnerablepower ///
	if ind7==1
eststo confidenceIndControls2


************************************************
* Generating Tables for Appendix
************************************************

cd "$path/Tables/"

esttab confidenceAllBase confidenceAllControls confidenceDemControls ///
	confidenceIndControls confidenceGOPControls ///
	using "CCES2018ExperimentAttributionOLSControls.tex", replace ///
	title("CCES Experiment of OLS Models of Confidence in Attribution, v1") ///
	label nobaselevels ///
	mtitles("Base" "Controls" "Democrats" "Independents" "Republicans")
	
esttab confidenceAllBase confidenceAllControls2 confidenceDemControls2 ///
	confidenceIndControls2 confidenceGOPControls2 ///
	using "CCES2018ExperimentAttributionOLSControls2.tex", replace ///
	title("CCES Experiment of OLS Models of Confidence in Attribution, v2") ///
	label nobaselevels ///
	mtitles("Base" "Controls" "Democrats" "Independents" "Republicans")

	
